package com.yixing.tech.wms.service.impl;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wms.entity.InStockWork;
import com.yixing.tech.wms.param.ContainerBindMaterialParam;
import com.yixing.tech.wms.param.ToApplyParam;
import com.yixing.tech.wms.service.RawMaterialInStockService;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.yixing.tech.common.CommonConstant.*;

/**
 * 原材料入库服务实现
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Slf4j
@Service
public class RawMaterialInStockServiceImpl extends AbstractInStockService implements RawMaterialInStockService {

    /**
     * 根据容器编码申请库位，申请当前空余的空位
     * 优先级排序：1. 无库存组。 有库存组：深度越大越优先
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Document applyLocationByContainerCode(String containerCode) throws BusinessException {
        return doApplyLocationByContainerCode(containerCode);
    }

    /**
     * 根据多个原材料入库单申请容器
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<InStockWork> applyContainerByInOrders(List<String> inOrders, String abc) throws BusinessException {
        return doApplyContainerByInOrders(inOrders, RAW_MATERIAL_IN_STOCK, RAW_MATERIAL_IN_STOCK_DETAIL, abc);
    }

    @Override
    public List<Document> listToApplyParam(ToApplyParam param) throws BusinessException {
        return doListToApplyParam(param, RAW_MATERIAL_IN_STOCK, RAW_MATERIAL_IN_STOCK_DETAIL);
    }

    /**
     * 查看申请成功的入库单明细
     */
    @Override
    public List<Document> listInStoking(ToApplyParam param) {
        return doListInStoking(param, Arrays.asList(InStockType.rawMaterialInStock.getCode(), InStockType.otherInStock.getCode()));
    }

    /**
     * 绑定物料到容器，申请库位，启动入库
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startStock(ContainerBindMaterialParam param) throws BusinessException {
        return doStartStock(param);
    }

    /**
     * 查询容器带绑定的信息
     */
    @Override
    public Object findContainerToBind(String containerCode) throws BusinessException {
        return doFindContainerToBind(containerCode);
    }

    /**
     * 查询容器带绑定的信息，通过库位
     */
    @Override
    public Object findContainerToBindByLocation(String locationCode) throws BusinessException {
        return doFindContainerToBindByLocation(locationCode);
    }

    /**
     * 查询容器带绑定的信息
     */
    @Override
    public Object findContainerToBindById(String bindId) throws BusinessException {
        return doFindContainerToBindById(bindId);
    }

    @Override
    public boolean reportErp(String dtlId) throws BusinessException {
        return doReportErp(dtlId, RAW_MATERIAL_IN_STOCK, RAW_MATERIAL_IN_STOCK_DETAIL);
    }

}
